<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\Position as PositionModel;
use think\facade\Request;
use think\Db;

class Position extends Manage
{

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $positionModel = new PositionModel();
            return $positionModel->tableData(input('param.'));
        }
        return $this->fetch();
    }


    /**
     *  职位添加
     * @return array|mixed
     */
    public function add()
    {
        $this->view->engine->layout(false);
        if(Request::isPost())
        {
            $positionModel = new PositionModel();
            return $positionModel->addData(input('param.'));
        }
        return $this->fetch();
    }


    /**
     *  职位修改
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $this->view->engine->layout(false);
        $positionModel = new PositionModel();
        if(Request::isPost())
        {
            return $positionModel->saveData(input('param.'));
        }
        $data = $positionModel->where('id',input('param.id/d'))->find();
        if (!$data) {
            return error_code(10002);
        }
        return $this->fetch('edit',['data' => $data]);
    }


    /**
     *  职位 软删除
     * User:tony
     * @return array
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg' => '删除失败',
            'data' => []
        ];

        $id  = input('post.id/d', '');

        if (!$id) {
            return $result;
        }

        // 判断当前图片是否被使用
        $sql = "select positionId from zl_user where positionId = '".$id."'";
    
        $list = Db::query($sql); 
        if (!$list || count($list) == 0) { 
            $positionModel = new PositionModel();
            if ($positionModel::destroy($id)) {
                $result['status'] = true;
                $result['msg'] = '删除成功';
            }
        } else { 
            $result['msg']    = '当前职位已使用无法删除！';
        }

        return $result;
    }

    /**
     * 获取所有职位
     */
    public function getAll()
    {
        $result     = [
            'status' => false,
            'msg'    => '获取失败',
            'data'   => [],
        ];
        $positionModel = new PositionModel();
        $positionList  = $positionModel->field('id,name,sort')->where([])->order('sort asc')->select();
        if (!$positionList->isEmpty()) {
            $result['data']   = $positionList->toArray();
            $result['status'] = true;
            $result['msg']    = '获取成功';
        }
        return $result;
    }

}